package selef.xiafan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import selef.xiafan.dto.OrderDTO;
import selef.xiafan.entity.Customer;
import selef.xiafan.entity.order.Order;
import selef.xiafan.entity.order.OrderItem;
import selef.xiafan.entity.order.Settlement;
import selef.xiafan.query.CustomerQuery;
import selef.xiafan.query.OrderQ;
import selef.xiafan.query.SettlementQuery;
import selef.xiafan.service.*;
import selef.xiafan.util.result.PageResult;
import selef.xiafan.util.result.Result;
import selef.xiafan.util.result.ResultFactory;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
@CrossOrigin
public class OrderController {
    @Autowired
    IOrderService iOrderService;
    @Autowired
    ICustomerService iCustomerService;
    @Autowired
    ICategoryService iCategoryService;
    @Autowired
    IOrderItemService iOrderItemService;
    @Autowired
    ISettleMentService iSettleMentService;

    @PostMapping("/OrderList")
    public PageResult orderList(@RequestBody OrderQ orderQ) {
        return iOrderService.orderList(orderQ);
    }

    @PostMapping("/customer_order")
    public PageResult customerOrderList(@RequestBody OrderQ orderQ) {
        return iOrderService.customerOrderList(orderQ);
    }

    @PostMapping("/add_update")
    public Result addAndUpdate(@RequestBody Order order) {
        Integer result = iOrderService.addOrUpdate(order);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("添加成功");
        }

        if (result == 2) {
            return ResultFactory.buildSuccessResult("修改成功");
        }
        return ResultFactory.buildFailResult("异常");
    }

    @GetMapping("/categoryList")
    public Result getCategory() {
        return ResultFactory.buildSuccessResult(iCategoryService.getAll());
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Order order) {
        Integer result = iOrderService.delete(order);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("删除成功");
        }
        return ResultFactory.buildFailResult("删除失败");
    }

    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Order> orderList) {
        Integer result = iOrderService.batchDelete(orderList);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("删除成功");
        }
        return ResultFactory.buildFailResult("删除失败");
    }

    /*
     * 提交录入订单，交由服务管理
     * */
    @PostMapping("/confirmOrder")
    public Result confirmOrder(@RequestBody Order order) {
        Integer result = iOrderService.confirmOrder(order);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("提交成功");
        }
        if (result == 2) {
            return ResultFactory.buildFailResult("订单已提交，请勿重复提交");
        }
        if (result == 3) {
            return ResultFactory.buildFailResult("订单正在处理中，请勿重复提交");
        }
        if (result == 4) {
            return ResultFactory.buildFailResult("订单以处理完毕，需要提交请重新录入");
        }
        return ResultFactory.buildFailResult("提交异常");
    }

    @PostMapping("/OrderItemList")
    public PageResult<Order> orderItem(@RequestBody OrderQ orderQ) {
        return iOrderService.orderItemList(orderQ);
    }

    /* ********************************** 订单详情 ********************************** */
    @PostMapping("/item/put")
    public Result itemAdd(@RequestBody OrderItem orderItem) {
        iOrderItemService.save(orderItem);
        return ResultFactory.buildSuccessResult("添加成功");
    }

    @PostMapping("/item/details")
    public Result details(@RequestBody Order order) {
        List<OrderItem> itemDetailByOrderId = iOrderItemService.getItemDetailByOrderId(order);
        return ResultFactory.buildSuccessResult(itemDetailByOrderId);
    }

    @PostMapping("/item/delete")
    public Result delete(@RequestBody OrderItem orderItem) {
        Integer result = iOrderItemService.deleteByOrderItem(orderItem);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("删除成功");
        } else if (result == 2) {
            return ResultFactory.buildFailResult("该信息不存在，请刷新页面或者联系管理员");
        } else {
            return ResultFactory.buildFailResult("信息异常，请重新登录");
        }
    }

    /**
     * 订单详情确定：
     * 计算总金额，改变状态
     * 添加结算订单信息
     * 修改库存
     */
    @PostMapping("/item/confirmItemDetail")
    public Result confirmItemDetail(@RequestBody OrderDTO orderDTO) {
        Order order = orderDTO.getOrder();
        List<OrderItem> orderItem = orderDTO.getOrderItem();

        Integer settleResult = iSettleMentService.save(order, orderItem);
        if (settleResult == 2) {
            return ResultFactory.buildFailResult("请重新提交");
        }
        if (settleResult == 1) {
            iOrderItemService.confirmOrderItemDetail(order);
            return ResultFactory.buildSuccessResult("提交成功");
        }
        return ResultFactory.buildFailResult("异常");
    }

    /**
     * 判断订单详情是否为空
     */
    @PostMapping("/item/checkItemIsNull")
    public Result checkItemIsNull(@RequestBody Order order) {
        Integer result = iOrderItemService.checkItemIsNull(order);
        if (result == 1) {
            return ResultFactory.buildFailResult("当前订单详情还没有添加数据，确认提交吗？");
        }
        return ResultFactory.buildSuccessResult("");
    }

    /* ********************************** 订单结算 ********************************** */
    /*
     * 结算订单列表
     * */
    @PostMapping("/settle/settleList")
    public PageResult<Settlement> settlementList(@RequestBody SettlementQuery settlementQuery) {
        return iSettleMentService.settlementList(settlementQuery);
    }

    /*
     * 确认结算
     * */
    @PostMapping("/settle/confirmPay")
    public Result confirmPay(@RequestBody Settlement requestSettlement) {
        Integer result = iSettleMentService.confirmPay(requestSettlement);
        if (result == 1) {
            return ResultFactory.buildSuccessResult("订单已确认支付");
        }
        return ResultFactory.buildFailResult("程序异常");
    }

    /* ********************************** 订单归还 ********************************** */
    @PostMapping("/getReturnList")
    public PageResult<Customer> getFinalList(@RequestBody CustomerQuery customerQuery) {
        return iCustomerService.getReturnList(customerQuery);
    }
}
